﻿using System;
using System.Numerics;
using UtilZ.Dotnet.Ex.Base;

namespace UtilZ.Dotnet.Ex.Normalization
{
#pragma warning disable 8500
#if CORE
    unsafe internal class InnerNormalizer_Half<TResult> : InnerNormalizerAbs<Half, TResult>
           where TResult : struct, IComparable<TResult>, INumberBase<TResult>
    {

        public InnerNormalizer_Half(NormalizerOptions options)
             : base(options, false)
        {

        }

        protected override TResult PrimitiveExcute(Half source, double factor, double inputOffset, double outputOffset)
        {
            return base.ConvertToResult(((double)source - inputOffset) * factor + outputOffset);
        }

        protected override void PrimitiveExcute(Half* sourcePtr, TResult* destPtr, int length, double outputOffset)
        {
            var minMax = EnumerableEx.FindMinMaxIndex(sourcePtr, length);
            double inputOffset;
            var factor = base.CaculateNormalizeFactor((double)minMax.MinValue.Value, (double)minMax.MaxValue.Value, out inputOffset);
            if (double.IsNaN(factor))
            {
                base.FactorNaNFill(destPtr, length);
            }
            else
            {
                for (int i = 0; i < length; i++)
                {
                    destPtr[i] = base.ConvertToResult(((double)sourcePtr[i] - inputOffset) * factor + outputOffset);
                }
            }
        }

    }
#endif
}
